package com.leetcode.algorithm.y22.m07.w2;

import com.leetcode.algorithm.common.ListNode;

/**
 * 82. 删除排序链表中的重复元素 II
 * 
 * https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/
 * 
 * @author jie.deng
 *
 */
class Question0082Solution01 {

	public ListNode deleteDuplicates(ListNode head) {
		ListNode sentinel = new ListNode(0, head);
		ListNode prev = sentinel;
		while (prev != null && prev.next != null) {
			ListNode cur = prev.next;
			int curVal = cur.val;
			while (cur.next != null && cur.next.val == curVal) {
				cur = cur.next;
			}
			if (cur == prev.next) {
				prev = prev.next;
			} else {
				prev.next = cur.next;
			}
		}
		return sentinel.next;
	}

}